package com.lzy.huijinjiazu.mvp.ui.adapter;

import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jess.arms.base.App;
import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.mvp.model.entity.MyCollection;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by long on 2018/1/15.
 */

public class CollectListAdapter extends DefaultAdapter<MyCollection> {
    private OnCheckClickListener onCheckClickListener;

    public CollectListAdapter() {
        super(new ArrayList<>());
    }

    public void setOnCheckClickListener(OnCheckClickListener listener) {
        this.onCheckClickListener = listener;
    }

    @Override
    public BaseHolder<MyCollection> getHolder(View v, int viewType) {
        return new CollectionHolder(v);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_collection;
    }

    public String getIds() {
        String ids = "";
        for (MyCollection mInfo : mInfos) {
            if (mInfo.isCheck()) {
                ids += mInfo.getGoodsId() + ",";
            }
        }
        return ids;
    }

    public class CollectionHolder extends BaseHolder<MyCollection> {
        @BindView(R.id.tv_bar)
        TextView mTvBar;
        @BindView(R.id.iv_goods_image)
        ImageView mIvGoodsImage;
        @BindView(R.id.tv_goods_name)
        TextView mTvGoodsName;
        @BindView(R.id.tv_market_price)
        TextView mTvMarketPrice;
        @BindView(R.id.tv_price)
        TextView mTvPrice;
        @BindView(R.id.cb_item)
        CheckBox mCbItem;
        @BindView(R.id.ll_bar)
        LinearLayout mLlBar;
        @BindView(R.id.cb_item_head)
        CheckBox mCbItemHead;
        private AppComponent mAppComponent;
        private ImageLoader mImageLoader;//用于加载图片的管理类,默认使用glide,使用策略模式,可替换框架

        public CollectionHolder(View itemView) {
            super(itemView);
            //可以在任何可以拿到Application的地方,拿到AppComponent,从而得到用Dagger管理的单例对象
            mAppComponent = ((App) itemView.getContext().getApplicationContext()).getAppComponent();
            mImageLoader = mAppComponent.imageLoader();
        }

        @Override
        public void setData(MyCollection data, int position) {
            List<MyCollection> infos = getInfos();
            MyCollection pre = null;
            if (position != 0) {
                pre = infos.get(position - 1);
            }
            if ((pre != null && pre.getInOneMomth() != data.getInOneMomth()) || position == 0) {
                mLlBar.setVisibility(View.VISIBLE);
                mTvBar.setVisibility(View.VISIBLE);
            } else {
                mLlBar.setVisibility(View.GONE);
                mTvBar.setVisibility(View.GONE);
            }

            mTvBar.setText(data.getInOneMomth() == 1 ? "一个月内" : "一个月前");
            mImageLoader.loadImage(mAppComponent.appManager().getCurrentActivity() == null
                            ? mAppComponent.application() : mAppComponent.appManager().getCurrentActivity(),
                    ImageConfigImpl
                            .builder()
                            .url(data.getGoodsImageUrl())
                            .imageView(mIvGoodsImage)
                            .build());
            mTvPrice.setText(Html.fromHtml(ArmsUtils.getString(itemView.getContext(), R.string.shopping_cart_money, "1298", "100")));
            mTvMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            mCbItem.setVisibility(data.isEdit() ? View.VISIBLE : View.GONE);
            mCbItemHead.setVisibility(data.isEdit() ? View.VISIBLE : View.GONE);
            mCbItem.setChecked(data.isCheck());
            boolean headCheck = true;
            for (int i = 0; i < mInfos.size(); i++) {
                if (data.getInOneMomth() == 0) {
                    if (mInfos.get(i).getInOneMomth() == 0) {
                        if (!mInfos.get(i).isCheck()) {
                            headCheck = false;
                            break;
                        }
                    }
                } else {
                    if (mInfos.get(i).getInOneMomth() == 1) {
                        if (!mInfos.get(i).isCheck()) {
                            headCheck = false;

                            break;
                        }

                    }
                }
            }
            mCbItemHead.setChecked(headCheck);
        }

        @OnClick({R.id.cb_item_head, R.id.cb_item})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.cb_item_head:
                    checkAllGroup();
                    onCheckClickListener.setOnGroupClick(getAdapterPosition(), mInfos.get(getAdapterPosition()).getInOneMomth(), mCbItemHead.isChecked());

                    notifyDataSetChanged();
                    break;
                case R.id.cb_item:
                    mInfos.get(getAdapterPosition()).setCheck(mCbItem.isChecked());
                    onCheckClickListener.setOnItemClick(mInfos.get(getAdapterPosition()).getInOneMomth(), mInfos.get(getAdapterPosition()).getGoodsId(), mCbItem.isChecked(), mCbItemHead);
                    break;
            }
        }

        private void checkAllGroup() {
            for (int i = 0; i < mInfos.size(); i++) {
                if (mInfos.get(getAdapterPosition()).getInOneMomth() == 0) {
                    if (mInfos.get(i).getInOneMomth() == 0) {
                        mInfos.get(i).setCheck(mCbItemHead.isChecked());
                    }
                } else {
                    if (mInfos.get(i).getInOneMomth() == 1) {
                        mInfos.get(i).setCheck(mCbItemHead.isChecked());
                    }
                }
            }
        }
    }

    public int getSelectCount() {
        int count = 0;
        for (MyCollection info : mInfos) {
            if (info.isCheck()) {
                count++;
            }
        }
        return count;
    }

    public interface OnCheckClickListener {
        void setOnItemClick(int inOneMomth, String id, boolean isCheck, CheckBox view);

        void setOnGroupClick(int position, int inOneMomth, boolean isCheck);

        void setOnCheckChange(int position, boolean b);

    }
}
